<?php

/**
 * Created by PhpStorm.
 * User: zhangxiang
 * Date: 2017/6/1 0001
 * Time: 上午 10:01
 */
class ArticleController
{
    private $articleService;
    private $diaryService;
    private $articleListCacheModel;

    function __construct()
    {
        $this->articleService           =   new ArticleService();
        $this->diaryService             =   new DiaryService();
        $this->articleListCacheModel    =   new ArticleListCacheModel();
        $this->actions                  =   array(
            'createArticle','editArticle','getArticleInfo','getUserArticleList',
            'getArticleList','deleteArticle','getAllArticleCategory'
        );
    }

    /**
     * 重载无访问的action
     * @param $functionName
     * @param $args
     * @return mixed
     */
    function __call($functionName,$args)
    {
        AC::$errorNo = 'SA004';
        AC::$errorMsg = ' Error : call action is not exists in ArticleController! ';
        return false;
    }

    /**
     * 路由
     * @param $param
     * @return bool|string
     */
    public function callApi($param)
    {
        return $this->$param['action']($param);
    }

    /**
     * 获取文章详情
     * @param $param
     * @return array|bool
     */
    public function getArticleInfo($param)
    {
        $author_uid        =   intval($param['author_uid']);
        $articleBodyId     =   intval($param['aid']);
        if ($articleBodyId < 1) {
            AC::$errorNo    =   'ATC001';
            return  FALSE;
        }

        $articleInfo   = $this->articleService->getArticleInfo($articleBodyId, $author_uid, AC::HOST_UID);
        if (AC::$errorNo) {
            return  FALSE;
        }
        //更新文章关联点滴计数
        $this->diaryService->countTimes(array('uid' => $articleInfo['uid'], 'bodyId' => $articleInfo['diaryId'], 'viewTimes' => 1));

        return $articleInfo;
    }

    /**
     * 创建文章
     * @param $param
     * @return array|bool
     */
    public function createArticle($param)
    {
        $uid = isset($param['uid']) ? intval($param['uid']) : '';
        $time = isset($param['time']) ? $param['time'] : '';
        $source = isset($param['source']) ? $param['source'] : 0;
        if(!$uid){
            AC::$errorNo    =   'ATC001';
            return  FALSE;
        }
        if($source == 0 && !empty($time) && strtotime($time) < AC::SERVER_TIME) {
            AC::$errorNo    =   'ATC003';           //定时发送时间不能早于当前时间
            return  FALSE;
        }
        if($source == 0 && empty($param['topic_id'])) {
            AC::$errorNo    =   'ATC004';           //后台编辑文章需要话题
            return  FALSE;
        }
        //参数过滤
        $param = $this->articleService->assembleArticleData($param);
        if(empty($param)) return false;

        //验证权限
        $access = $this->articleService->checkPermission($param['uid']);
        if(!$access) return false;

        $articleInfo = $this->articleService->createArticle($param);
        if($articleInfo) {
            //文章进点滴
            $diaryInfo = $this->diaryService->articleIntoDiary($articleInfo, $uid , $param['topic_id'], $time);
            //将点滴diaryId加入到article表中
            //更新缓存
            $this->articleService->updateArticle($uid, $articleInfo['aid'], ['diaryId' => $diaryInfo['bodyId']]);
            $articleInfo['diaryId'] = strval($diaryInfo['bodyId']);
            $diaryListModel = new DiaryListModel();
            $articleInfo['topics'] = $diaryListModel->getDiaryTopic($diaryInfo['topics']);
            if(!empty($time) && strtotime($time) > AC::SERVER_TIME) {
                $this->articleListCacheModel->setArticleTimedList($diaryInfo['bodyId'], strtotime($time));
            }
        }
        return $articleInfo;
    }

    /**
     * 删除文章
     * @param $param
     * @return bool
     */
    public function deleteArticle($param){
        $author_uid = isset($param['author_uid']) ? intval($param['author_uid']) : '';
        $articleBodyId = isset($param['aid']) ? intval($param['aid']) : '';
        if($articleBodyId < 1 || $author_uid < 1) {
            AC::$errorNo    =   'ATC002';
            return  FALSE;
        }
        $result = $this->articleService->deleteArticle($articleBodyId, $author_uid);
        return $result;
    }

    /**
     * 编辑文章
     * @param $param
     * @return array|bool
     */
    public function editArticle($param)
    {
        $author_uid = isset($param['author_uid']) ? intval($param['author_uid']) : '';
        $articleBodyId = isset($param['aid']) ? intval($param['aid']) : '';
        if($articleBodyId < 1 || $author_uid < 1) {
            AC::$errorNo    =   'ATC002';
            return  FALSE;
        }
        $result = $this->articleService->editArticle($param);
        if($result && $param['topic_id']) {
            $this->diaryService->updateDiaryTopic($result['diaryId'], $author_uid , $param['topic_id']);
        }
        return $result;
    }

    /**
     * 返回所有文章分类
     * @return array
     */
    public function getAllArticleCategory(){
        return $this->articleService->getAllArticleCategory();
    }



    /**
     * 获取用户文章列表
     * @param $param
     * @return array|bool
     */
    public function getUserArticleList($param){
        $uid = isset($param['uid']) ? intval($param['uid']) : '';
        $id = isset($param['id']) ? intval($param['id']) : '';
        $length = isset($param['length']) ? intval($param['length']) : '';
        if (empty($uid))
        {
            AC::$errorNo    =   'ATC001';
            return  FALSE;
        }

        $list = $this->articleService->getUserArticleList($uid, $id, $length);
        if(empty($list)) return (object)array();
        $result = [
            'list'  => $list,
            'count' => count($list)
        ];
        return  $result;
    }

    /**
     * 获取文章列表
     * @param $param
     * @return array|bool
     */
    public function getArticleList($param){
        $type = isset($param['type']) ? strval($param['type']) : '';
        $id = isset($param['id']) ? intval($param['id']) : '';
        $length = isset($param['length']) ? intval($param['length']) : '';
        if(empty($length)) $length = 20;
        $list = $this->articleService->getArticleList($type, $id, $length);
        if(empty($list)) return (object)array();
        $result = [
            'list'  => $list,
            'count' => count($list)
        ];
        return  $result;
    }

}